﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class TangCaBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();
        public void Them(TANGCA tc)
        {
            DB.TANGCAs.InsertOnSubmit(tc);
            DB.SubmitChanges();
        }

        public bool KiemTraTonTai(DateTime ngay, int ma)
        {
            if (DB.TANGCAs.Any(a => a.Ngay == ngay && a.MaNV == ma))
                return true;
            else
                return false;
        }

        public TANGCA LayTC(DateTime ngay, int manv)
        {
            TANGCA tc = DB.TANGCAs.SingleOrDefault(a => a.Ngay == ngay && a.MaNV == manv);
            return tc;
        }

        public Double LayGioTC(DateTime ngay, int manv)
        {
            TANGCA tc = DB.TANGCAs.SingleOrDefault(a => a.Ngay == ngay && a.MaNV == manv);
            return (Double)(tc.ThoiGian * tc.HeSo);
        }

        public List<TANGCA2> LayGioTC2(int _thang, int _nam, int _manv)
        {
            string thang;
            if (_thang > 9)
                thang = _nam.ToString() + "-" + _thang.ToString();
            else
                thang = _nam.ToString() + "-0" + _thang.ToString();

            
            List<TANGCA2> tc = (from a in DB.TANGCAs
                                where a.MaNV == _manv && a.Ngay.ToString().Contains(@thang)
                                select new TANGCA2()
                                {
                                    Ngay = a.Ngay,
                                    ThoiGian = a.ThoiGian.Value
                                }).ToList();
            return tc;
        }

        public void Sua(TANGCA n)
        {
            TANGCA o = DB.TANGCAs.SingleOrDefault(i => i.Ngay == n.Ngay && i.MaNV == n.MaNV);
            if (o == null)
            {
                Them(n);
            }
            else
            {
                o.ThoiGian = n.ThoiGian;
                o.HeSo = n.HeSo;
                o.TrangThaiChot = n.TrangThaiChot;
                DB.SubmitChanges();
            }
        }

        public void Xoa(DateTime ngay, int manv)
        {
            TANGCA a = DB.TANGCAs.SingleOrDefault(i => i.Ngay == ngay && i.MaNV == manv);
            if (a == null)
            {
            }
            else
            {
                DB.TANGCAs.DeleteOnSubmit(a);
                DB.SubmitChanges();
            }
        }
    }
}
